//
//  Weather.m
//  ToWhere
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "Weather.h"
@implementation Weather
- (void)dealloc
{
    [_weatherTwo release];
    [_weatherThree release];
    [_dayImage release];
    [_nightImage release];
    [_dayLabel release];
    [_nightLabel release];
    [_weekLabel release];
    [_wind release];
    [_windpwd release];
    [_dateLabel release];
    [_dayTemp release];
    [_nightTemp release];
    [_tempDayImage release];
    [_tempNightImage release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
       // [self createView];
        
    }
    return self;
}
- (void)setWeatherTwo:(WeatherModelTwo *)weatherTwo{

    if (_weatherTwo != weatherTwo) {
        [_weatherTwo release];
        _weatherTwo = [weatherTwo retain];
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yy-MM-dd"];
    NSString *string = self.weatherTwo.ddate;
    NSDate *dateStr =[formatter dateFromString:string];
    
    self.weekLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15 *kHeight, 62.5 *kWidth, 30 *kHeight)];
    self.weekLabel.font = [UIFont systemFontOfSize:17 *kWidth];
    self.weekLabel.textColor = [UIColor whiteColor];
    self.weekLabel.textAlignment = NSTextAlignmentCenter;
    self.weekLabel.text = [[self class]weekdayStringFromDate:dateStr];
    [self addSubview:_weekLabel];
    [_weekLabel release];
    
    self.dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50 *kHeight, 62.5 *kWidth, 30 *kHeight)];
    self.dayLabel.textAlignment = NSTextAlignmentCenter;
    self.dayLabel.font = [UIFont systemFontOfSize:13 *kWidth];
    self.dayLabel.textColor = [UIColor whiteColor];
    self.dayLabel.text = self.weatherTwo.dayweather;
    [self addSubview:_dayLabel];
    [_dayLabel release];
    
    self.dayImage = [[UIImageView alloc] initWithFrame:CGRectMake(10 *kWidth, 85 *kHeight, 42.5 *kWidth, 40 *kHeight)];
    
       NSString *imageName = @"";
    if ([self.weatherTwo.dayweather containsString:@"阴"]) {
        imageName = @"weathericon_3阴@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"晴"]){
        imageName = @"weathericon_1晴日@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"多云"]){
        
        imageName = @"weathericon_2多云日@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"霾"]){
        
        imageName = @"weathericon_6霾@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"小雨"]){
        imageName = @"weathericon_7小雨@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"中雨"]){
        
        imageName = @"weathericon_8中雨@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"大雨"]){
        
        imageName = @"weathericon_9大雨@2x";
    }else if([self.weatherTwo.dayweather containsString:@"阵雨"]){
        imageName = @"weathericon_13阵雨日@2x";
    }else if ([self.weatherTwo.dayweather containsString:@"冰雹"]){
        
        imageName = @"weathericon_17冰雹@2x";
        
    }else if ([self.weatherTwo.dayweather containsString:@"雷"]){
        
        imageName = @"weathericon_15雷电@2x";
    }else if ([self.weatherTwo.dayweather containsString:@"雨夹雪"]){
        
        
        imageName = @"weathericon_18雨夹雪@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"小雪"]){
        
        imageName = @"weathericon_19小雪@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"中雪"]){
        
        imageName = @"weathericon_20中雪@2x";
    }else if ([self.weatherTwo.dayweather containsString:@"大雪"]){
        
        imageName = @"weathericon_21大雪@2x";
    }else if ([self.weatherTwo.dayweather containsString:@"暴雪"]){
        
        imageName = @"weathericon_22暴雪@2x";
    }else if ([self.weatherTwo.dayweather containsString:@"阵雪"]){
        imageName = @"weathericon_23阵雪@2x";
    }
    else if ([self.weatherTwo.dayweather containsString:@"风"]){
        
        imageName = @"weathericon_37大风@2x";
    }else if ([self.weatherTwo.dayweather containsString:@"台风"]){
        
        imageName = @"weathericon_38台风@2x";
    }else if ([self.weatherTwo.dayweather containsString:@"龙卷风"]){
        
        imageName = @"weathericon_40龙卷风@2x";
    }else if ([self.weatherTwo.dayweather containsString:@"雾"]){
        
        imageName = @"weathericon_4雾@2x";
    }
    
    self.dayImage.image = [UIImage imageNamed:imageName];
    
    [self addSubview:_dayImage];
    [_dayImage release];
    
    self.dayTemp = [[UILabel alloc] initWithFrame:CGRectMake(0, 120 *kHeight, 62.5 *kWidth, 20 *kHeight)];
    self.dayTemp.textColor = [UIColor whiteColor];
    self.dayTemp.textAlignment = NSTextAlignmentCenter;
    self.dayTemp.font = [UIFont systemFontOfSize:16 *kWidth];
    self.dayTemp.text = weatherTwo.hightemp;
    [self addSubview:_dayTemp];
    [_dayTemp release];
    self.tempDayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sanjiaox"]];
    self.tempDayImage.frame = CGRectMake(15 *kWidth, 145 *kHeight, 32.5 *kWidth, 25 *kHeight);
    [self addSubview:_tempDayImage];
    [_tempDayImage release];

    
    
    self.nightImage = [[UIImageView alloc] initWithFrame:CGRectMake(10 *kWidth, 230 *kHeight, 42.5 *kWidth, 40 *kHeight)];
    
    NSString *imageNameTo = nil;
    if ([self.weatherTwo.nightweather containsString:@"阴"]) {
        imageNameTo = @"weathericon_3阴@2x";
    }
    else if ([self.weatherTwo.nightweather containsString:@"晴"]){
        imageNameTo = @"weathericon_1晴日@2x";
    }
    else if ([self.weatherTwo.nightweather containsString:@"多云"]){
        
        imageNameTo = @"weathericon_2多云日@2x";
    }
    else if ([self.weatherTwo.nightweather containsString:@"霾"]){
        
        imageNameTo = @"weathericon_6霾@2x";
        
    }
    else if ([self.weatherTwo.nightweather containsString:@"小雨"]){
        imageNameTo = @"weathericon_7小雨@2x";
    }
    else if ([self.weatherTwo.nightweather containsString:@"中雨"]){
        
        imageNameTo = @"weathericon_8中雨@2x";
    }
    else if ([self.weatherTwo.nightweather containsString:@"大雨"]){
        
        imageNameTo = @"weathericon_9大雨@2x";
    }else if([self.weatherTwo.nightweather containsString:@"阵雨"]){
        imageNameTo = @"weathericon_13阵雨日@2x";
    }else if ([self.weatherTwo.nightweather containsString:@"冰雹"]){
        
        imageNameTo = @"weathericon_17冰雹@2x";
        
    }else if ([self.weatherTwo.nightweather containsString:@"雷"]){
        
        imageNameTo = @"weathericon_15雷电@2x";
    }else if ([self.weatherTwo.nightweather containsString:@"雨夹雪"]){
        
        imageNameTo = @"weathericon_18雨夹雪@2x";
    }
    else if ([self.weatherTwo.nightweather containsString:@"小雪"]){
        
        imageNameTo = @"weathericon_19小雪@2x";
    }
    else if ([self.weatherTwo.nightweather containsString:@"中雪"]){
        
        imageNameTo = @"weathericon_20中雪@2x";
    }else if ([self.weatherTwo.nightweather containsString:@"大雪"]){
        
        imageNameTo = @"weathericon_21大雪@2x";
    }else if ([self.weatherTwo.nightweather containsString:@"暴雪"]){
        
        imageNameTo = @"weathericon_22暴雪@2x";
    }else if ([self.weatherTwo.nightweather containsString:@"阵雪"]){
        imageNameTo = @"weathericon_23阵雪@2x";
    }
    else if ([self.weatherTwo.nightweather containsString:@"风"]){
        
        imageNameTo = @"weathericon_37大风@2x";
    }else if ([self.weatherTwo.nightweather containsString:@"台风"]){
        
        imageNameTo = @"weathericon_38台风@2x";
    }else if ([self.weatherTwo.nightweather containsString:@"龙卷风"]){
        
        imageNameTo = @"weathericon_40龙卷风@2x";
    }else if ([self.weatherTwo.nightweather containsString:@"雾"]){
        
        imageNameTo = @"weathericon_4雾@2x";
    }
    
    self.nightImage.image = [UIImage imageNamed:imageNameTo];
    
    [self addSubview:_nightImage];
    [_nightImage release];
    
    self.tempNightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sanjiaos"]];
    self.tempNightImage.frame = CGRectMake(15 *kWidth, 180 *kHeight, 32.5 *kWidth, 25 *kHeight);
    [self addSubview:_tempNightImage];
    [_tempNightImage release];
    
    self.nightTemp = [[UILabel alloc] initWithFrame:CGRectMake(0, 210 *kHeight, 62.5 *kWidth, 20 *kHeight)];
    self.nightTemp.font = [UIFont systemFontOfSize:16 *kWidth];
    self.nightTemp.textColor = [UIColor whiteColor];
    self.nightTemp.textAlignment = NSTextAlignmentCenter;
    self.nightTemp.text = weatherTwo.lowtemp;
    [self addSubview:_nightTemp];
    [_nightTemp release];
    self.nightLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 275 *kHeight, 62.5 *kWidth, 30 *kHeight)];
    self.nightLabel.textColor = [UIColor whiteColor];
    self.nightLabel.textAlignment = NSTextAlignmentCenter;
    self.nightLabel.font = [UIFont systemFontOfSize:13 *kWidth];
    self.nightLabel.text = self.weatherTwo.nightweather;
    [self addSubview:_nightLabel];
    [_nightLabel release];
    
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 310 *kHeight, 62.5 *kHeight, 20 *kHeight)];
    self.dateLabel.textAlignment = NSTextAlignmentCenter;
    self.dateLabel.textColor = [UIColor whiteColor];
    self.dateLabel.font = [UIFont systemFontOfSize:17 *kWidth];
    self.dateLabel.text = [self.weatherTwo.ddate substringFromIndex:5];
    [self addSubview:_dateLabel];
    [_dateLabel release];
    
    [formatter release];
}
- (void)setWeatherThree:(WeatherModelThree *)weatherThree{


    if (_weatherThree != weatherThree) {
        [_weatherThree release];
        _weatherThree = [weatherThree retain];
    }
    self.wind = [[UILabel alloc] initWithFrame:CGRectMake(0, 335 *kHeight, 62.5 *kWidth, 30 *kHeight)];
    self.wind.textAlignment = NSTextAlignmentCenter;
    self.wind.textColor = [UIColor grayColor];
    self.wind.font = [UIFont systemFontOfSize:15 *kWidth];
    self.wind.text = weatherThree.nightwinddir;
    [self addSubview:_wind];
    [_wind release];
    
    self.windpwd = [[UILabel alloc] initWithFrame:CGRectMake(0, 370 *kHeight, 62.5 *kWidth, 20 *kHeight)];
    self.windpwd.textAlignment = NSTextAlignmentCenter;
    self.windpwd.font = [UIFont systemFontOfSize:15 *kWidth];
    self.windpwd.textColor = [UIColor grayColor];
    self.windpwd.text = weatherThree.nightwindpwd;
    [self addSubview:_windpwd];
    [_windpwd release];


}
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}
@end
